package com.platform.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.platform.common.config.WxPayConfig;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.AlgorithmParameterSpec;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class WxCode {
    private static WxCode instance;
    public static WxCode getInstance() {
        if (instance == null) {
            instance = new WxCode();
        }
        return instance;
    }

    public String getAccessToken() {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>();
        params.put("APPID", "wxae0f0d0aef1a8645");
        params.put("APPSECRET", "26fd8e0d4fe102b8e37fc421f1277684");
        ResponseEntity<String> reponseEntity = restTemplate.getForEntity("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
        String body = reponseEntity.getBody();
        JSONObject object = JSON.parseObject(body);
        String Access_Token = object.getString("access_token");
        return Access_Token;
    }

    public String getOpenId(String code) {
        String appid = "wxae0f0d0aef1a8645";//WxPayConfig.appid;
        String secret = "26fd8e0d4fe102b8e37fc421f1277684";//WxPayConfig.appsecret;
        String wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
        String param = "appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
        String jsonString = GetPostUntil.sendGet(wxLoginUrl, param);
        log.info("jsonString:"+jsonString);
        JSONObject json = JSONObject.parseObject(jsonString);
        System.out.println(json);
        String openid = json.getString("openid");
        log.info("openid:"+openid);
        return openid;
    }

    public String wxDecrypt(String encrypted, String iv, String sessionKey) throws Exception {
        java.security.Security.addProvider(new BouncyCastleProvider());
        org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
        byte[] encrypData = base64.decodeBase64(encrypted);
        byte[] ivData = base64.decodeBase64(iv);
        byte[] sKey = base64.decodeBase64(sessionKey);
        String decrypt = decrypt(sKey,ivData,encrypData);
        return decrypt;
    }

    public String decrypt(byte[] key, byte[] iv, byte[] encData) throws Exception {
        //解析解密后的字符串
        String resultString = null;
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            resultString = new String(cipher.doFinal(encData), "UTF-8");
        } catch (Exception e) {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            resultString = new String(cipher.doFinal(encData), "UTF-8");
        }
        return resultString;
    }
}
